# -*- coding:utf-8 -*-
from tornado import web, gen
import os

from appcommon.framework import APIRequestHandler
from appcommon import checktool
from common import decorator
from common import utiltool
import models as adm
from common import timetool
import settings


class AdListHandler(APIRequestHandler):
    """广告列表"""
    @checktool.check_sign
    @gen.coroutine
    def post(self):
        ad_type = int(self.get_argument('ad_type', '0'))
        info = yield adm.get_ad_list(ad_type)
        res = {"code": "0", "info": info}
        utiltool.response_write(self, res)


class AdContentHandler(APIRequestHandler):
    @decorator.check_params(['ad_id'])
    @checktool.check_sign
    @gen.coroutine
    def post(self):
        ad_id = self.get_argument('ad_id', '')
        res = yield adm.update_ad_view_cnt(ad_id)
        if res:
            info = yield adm.get_ad_content(ad_id)
            res = {"code": "0", "info": info}
        else:
            res = {"code": "1003", "msg": u"服务器开小差了,请稍后重试"}
        utiltool.response_write(self, res)